package com.milhouz.wormdroid.gl.model;

import javax.microedition.khronos.opengles.GL10;

import simpull.Vector2f;

import com.milhouz.wormdroid.gl.factory.BotAnimationFactory;
import com.milhouz.wormdroid.gl.model.anim.Animation;
import com.milhouz.wormdroid.gl.model.anim.TexRect;

/**
 * Wormdroid bot sprite
 */
public class BotGLSprite extends GLSprite {

	private final static int DEFAULT_BOT_MASS = 1;
	private final static float DEFAULT_BOT_ELASTICITY = 0.1f;
	private final static float DEFAULT_BOT_FRICTION = 0.5f;
	private final static boolean DEFAULT_BOT_FIXED = false;

	private int mTeamId;

	// bot orientation (1=right, 2=left, 3=up) 
	public boolean isSelectedBot = false;
	private boolean isJump = false;
	public int botOrientation = 1;

	// bot animations
	private Animation[] botAnims;
	
	private int mAnimTextureBotX = 0;
	private int mAnimTextureBotY = 0;
	
	public BotGLSprite(int id, int teamId, int x, int y, int width, int height, boolean isFixed, float mass, float elasticity, float friction, int resourceId) {
		super(id, x, y, width, height, DEFAULT_BOT_FIXED, DEFAULT_BOT_MASS, DEFAULT_BOT_ELASTICITY, DEFAULT_BOT_FRICTION, resourceId);
		mTeamId = teamId;
		botAnims = BotAnimationFactory.getInstance().getMarioAnims();
	}

	public int getTeamId() {
		return mTeamId;
	}

	@Override
	public void draw(GL10 gl, int zx, int zy, boolean isZoomed, float zoomFactor) {
		if (!isHide) {
			gl.glBindTexture(GL10.GL_TEXTURE_2D, getTextureName());
			if (isSelectedBot && isMoving()) {
				animate(gl);
			} else {
				waitAnimate(gl);
			}
			// Draw using verts
			gl.glPushMatrix();
			gl.glLoadIdentity();
			if (isZoomed)
				gl.glScalef(zoomFactor, zoomFactor, 1.0f);
			gl.glTranslatef(getGLX() + zx, getGLY() - zy, Z);
			mGrid.draw(gl, true);
			gl.glPopMatrix();
		}
	}

	private void animate(GL10 gl) {
		TexRect texRect = null;
		for (int i=0; i<botAnims.length; i++) {
			if (!isJump && isFalling()
					&& botAnims[i].mAnimationName.equalsIgnoreCase("STOP_RIGHT")) {
				texRect = botAnims[i].next();
				break;
			}
			if (botOrientation == 1 && !isJump
					&& botAnims[i].mAnimationName.equalsIgnoreCase("STOP_RIGHT")) {
				texRect = botAnims[i].next();
				break;
			} 
			if (botOrientation == 2 && !isJump
					&& botAnims[i].mAnimationName.equalsIgnoreCase("STOP_LEFT")) {
				texRect = botAnims[i].next();
				break;
			}
			if (isJump) {
				if (botOrientation == 1 && botAnims[i].mAnimationName.equalsIgnoreCase("JUMP_RIGHT")) {
					texRect = botAnims[i].next();
					break;
				}
				if (botOrientation == 2 && botAnims[i].mAnimationName.equalsIgnoreCase("JUMP_LEFT")) {
					texRect = botAnims[i].next();
					break;
				}
			}
		}
		if (texRect != null)
			mGrid.setTextureGrid(gl, texRect);
	}
	
	private void waitAnimate(GL10 gl) {
		// TODO a adapter
		if (botOrientation == 1) {
			mGrid.setTextureGrid(gl, botAnims[0].getFirstTexRect());
		} else {
			mGrid.setTextureGrid(gl, botAnims[1].getFirstTexRect());
		}
	}
	
	// --- BOT MOVES
	public void moveLeft(TargetGLSprite currentBotTarget) {
			this.botOrientation = 2;
			if (currentBotTarget != null && !currentBotTarget.isLaunched) {
				currentBotTarget.moveLeftWeaponTarget((int)this.getX(), (int)this.getY());
				this.setVelocity(new Vector2f(-4f, 0f));
			} else if (!isJump) {
				this.setVelocity(new Vector2f(-4f, 0f));
			}
	}
	
	public void moveRight(TargetGLSprite currentBotTarget) {
			this.botOrientation = 1;
			if (currentBotTarget != null && !currentBotTarget.isLaunched) {
				int botX = (int) this.getX();
				int botY = (int) this.getY();
				currentBotTarget.moveRightWeaponTarget(botX, botY);
				this.setVelocity(new Vector2f(3f, 0f));
			} else if (!isJump) {
				this.setVelocity(new Vector2f(3f, 0f));
			}
	}
	
	public void jump() {
		if (!isJump) {
			isJump = true;
			if (botOrientation == 1) {
				this.setVelocity(new Vector2f(4f, 10f));
			} else if (botOrientation == 2) {
				this.setVelocity(new Vector2f(-4f, 10f));
			}
		}
	}
	
	public boolean isMoving() {
		if (getVelocity().x > 0.3 || getVelocity().x < -0.3){
			return true;
		}else{
			isJump = false;
			return false;
		}
	}
	
	public boolean isFalling() {
		if (getVelocity().y < -0.5){
			return true;
		}else{
			return false;
		}
	}
}
